package com.itheima.web;

import com.itheima.pojo.PurchaseHistory;
import com.itheima.pojo.User;
import com.itheima.service.ItemService;
import com.itheima.service.PurchaseHistoryService;
import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/purchaseHistoryServlet")
public class PurchaseHistoryServlet extends HttpServlet {
    private ItemService itemService = new ItemService();
    private PurchaseHistoryService purchaseHistoryService = new PurchaseHistoryService();
    private UserService userService = new UserService();

    public class PurchaseHistoryWithName {
        // 商品id
        private Integer itemId;
        // 买家id
        private Integer buyerId;
        // 买家昵称
        private String buyerName;
        // 商品名称
        private String itemName;

        public PurchaseHistoryWithName() {
        }

        public PurchaseHistoryWithName(Integer itemId, Integer buyerId, String buyerName, String itemName) {
            this.itemId = itemId;
            this.buyerId = buyerId;
            this.buyerName = buyerName;
            this.itemName = itemName;
        }

        public Integer getItemId() {
            return itemId;
        }

        public void setItemId(Integer itemId) {
            this.itemId = itemId;
        }

        public Integer getBuyerId() {
            return buyerId;
        }

        public void setBuyerId(Integer buyerId) {
            this.buyerId = buyerId;
        }

        public String getBuyerName() {
            return buyerName;
        }

        public void setBuyerName(String buyerName) {
            this.buyerName = buyerName;
        }

        public String getItemName() {
            return itemName;
        }

        public void setItemName(String itemName) {
            this.itemName = itemName;
        }

        @Override
        public String toString() {
            return "PurchaseHistoryWithName{" +
                    "itemId=" + itemId +
                    ", buyerId=" + buyerId +
                    ", buyerName='" + buyerName + '\'' +
                    ", itemName='" + itemName + '\'' +
                    '}';
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过session获取user对象
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        Integer sellerId = user.getId();

        List<PurchaseHistory> purchaseHistories = purchaseHistoryService.selectBySellerId(sellerId);

        List<PurchaseHistoryWithName> purchaseHistoryWithNames = new ArrayList<>();
        for (PurchaseHistory purchaseHistory : purchaseHistories) {
            String buyerName = userService.selectById(purchaseHistory.getBuyerId()).getUsername();
            String itemName = itemService.selectById(purchaseHistory.getItemId()).getItemName();
            PurchaseHistoryWithName purchaseHistoryWithName = new PurchaseHistoryWithName(purchaseHistory.getItemId(), purchaseHistory.getBuyerId(), buyerName, itemName);
            purchaseHistoryWithNames.add(purchaseHistoryWithName);
        }

        request.setAttribute("purchaseHistoryWithNames", purchaseHistoryWithNames);

        request.getRequestDispatcher("/purchase-history.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
